#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <set>
using namespace std;

template <class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

template<>
struct HashFunc<string>            // ��ƫ�ػ�Ϊstring
{
	size_t operator()(const string& key)  // ��stringת��Ϊsize_t
	{
		size_t hash = 0;
		for (auto e : key)
		{
			hash *= 31;
			hash += e;
		}
		cout << key << ":" << hash << endl;
		return hash;
	}
};
/////////////////////////////////////////////////////

namespace open_address             // ��ɢ�У����Ŷ�ַ��
{
	enum Status
	{
		EMPTY,
		EXIST,
		DELETE
	};

	template <class K, class V>
	struct HashData
	{
		pair<K, V> _kv;
		Status _s;
	};
	
	template <class K, class V, class Hash = HashFunc<K>>
	class HashTable
	{
	public:

		HashTable()
		{
			_tables.resize(10);
		}

		bool Insert(const pair<K, V>& kv)
		{

			if (Find(kv.first))         // �����ͬ��ֵ���룬�ͻ᲻�ɹ�
				return false;

			// ʹ�ø����������ж��Ƿ�����  0.7������
			if (_n * 10 / _tables.size() == 7)
			{

				size_t newSize = _tables.size() * 2;        // ��������������
				HashTable<K, V, Hash> newHT;
				newHT._tables.resize(newSize);

				for (size_t i = 0; i < _tables.size(); i++)
				{
					if (_tables[i]._s == EXIST)
						newHT.Insert(_tables[i]._kv);
				}

				_tables.swap(newHT._tables);
			}
			Hash hf;
			size_t hashi = hf(kv.first) % _tables.size();

			while (_tables[hashi]._s == EXIST)
			{
				hashi++;

				hashi %= _tables.size();
			}
			_tables[hashi]._kv = kv;
			_tables[hashi]._s = EXIST;
			_n++;
			return true;
		}

		HashData<K, V>* Find(const K& key)   // ��������K��string����û�а취ȡģ
		{
			Hash hf;
			size_t hashi = hf(key) % _tables.size();
			while (_tables[hashi]._s != EMPTY)
			{
				if (_tables[hashi]._s == EXIST && key == _tables[hashi]._kv.first)
					return &_tables[hashi];
				hashi++;
				hashi %= _tables.size();
			}
			return nullptr;
		}

		// αɾ����
		bool Erase(const K& key)
		{
			HashData<K, V>* ret = Find(key);
			if (ret)
			{
				ret->_s = DELETE;
				--_n;
				return true;
			}
			return false;
		}

		void Print()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				if (_tables[i]._s == EXIST) 
				{
					//printf("[%d]->%d\n", i, _tables[i]._kv.first);
					cout << "[" << i << "]" << "->" << _tables[i]._kv.first << ":" << _tables[i]._kv.second << endl;
				}
				else if (_tables[i]._s == EMPTY)
				{
					printf("[%d]->\n", i);
				}
				else
				{
					printf("[%d]->D\n", i);
				}
			}
			cout << endl;
		}
	private:
		vector<HashData<K, V>> _tables;
		size_t _n = 0;
	};
	void test_open_address1()
	{
		HashTable<int, int> ht;
		int a[] = { 1, 4, 3, 24, 34, 6, 8, 9};
		for (auto e : a)
		{
			ht.Insert(make_pair(e, e));
		}
		ht.Print();

		ht.Erase(3);
		ht.Print();

		ht.Insert(make_pair(3, 3));
		ht.Print();
	}
	void test_open_address2()
	{
		string arr[] = { "ƻ��", "�㽶", "����", "ƻ��" };
		HashTable<string, int> countMap;
		for (auto& e : arr)
		{
			auto ret = countMap.Find(e);
			if (ret)
				ret->_kv.second++;
			else
				countMap.Insert(make_pair(e, 1));
		}
		countMap.Print();

		countMap.Insert(make_pair("abc", 1));
		countMap.Insert(make_pair("acb", 1));

	}
}

namespace hash_bucket
{
	template <class K, class V>
	struct HashNode
	{
		HashNode(const pair<K, V> kv)
			:_kv(kv)
			,_next(nullptr)
		{
		}
		pair<K, V> _kv;
		HashNode* _next;
	};

	template <class K, class V, class Hash = HashFunc<K>>
	class HashTable
	{
		typedef HashNode<K, V> Node;

	public:
		HashTable()
		{
			_tables.resize(10);
		}

		bool Insert(const pair<K, V>& kv)
		{
			if (Find(kv.first))
				return false;

			if (_n == _tables.size())         // ����������ӵ���1�ˣ��ͽ������ݣ���ͳд��
			{
				//size_t newSize = _tables.size() * 2;
				//HashTable<K, V> newHT;
				//newHT._tables.resize(newSize);
				//
				//// �����ɱ�
				//for (int i = 0; i < _tables.size(); i++)
				//{
				//	Node* cur = _tables[i];
				//	while (cur)
				//	{
				//		newHT.Insert(cur->_kv);
				//		cur = cur->_next;
				//	}
				//}
				//_tables.swap(newHT._tables);
				vector<Node*> newTables;
				newTables.resize(_tables.size() * 2, nullptr);
				for (int i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)       // ���ڵ�һ��һ�������������±�������ӳ���λ�ò���
					{
						Node* next = cur->_next;
						size_t hashi = hf(cur->_kv.first) % newTables.size();

						cur->_next = newTables[i];
						newTables[i] = cur;

						cur = next;
					}

					_tables[i] = nullptr;    
				}
				_tables.swap(newTables);
			}

			size_t hashi = hf(kv.first) % _tables.size();   // ������Ǹ�Ͱ
			Node* newnode = new Node(kv);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;

			return true;
		}

		Node* Find(const K& key)
		{
			
			size_t hashi = hf(key) % _tables.size();
			Node* cur = _tables[hashi];

			while (cur)
			{
				if (cur->_kv.first == key)
					return cur;
				cur = cur->_next;

			}

			return nullptr;
		}

		bool Erase(const K& key)
		{

			size_t hashi = hf(key) % _tables.size();
			Node* cur = _tables[hashi];
			Node* prev = nullptr;

			while (cur)
			{
				if (cur->_kv.first == key)
				{
					if (prev == nullptr)     // ���ǰһ��Ϊ�գ�˵����ͷɾ
					{
						_tables[hashi] = nullptr;
					}
					else
					{
						prev->_next = cur->_next;
					}
					delete cur;
					return true;
				}

				prev = cur;
				cur = cur->_next;
			}

			return false;
		}

		void Some()
		{
			size_t bucketSize = 0;
			size_t maxBucketLen = 0;             // ��Ͱ����󳤶�
			size_t sum = 0;
			double averageBucketLen = 0;
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
					bucketSize++;
				size_t bucketLen = 0;
				while (cur)
				{
					bucketLen++;
					cur = cur->_next;
				}

				sum += bucketLen;
				if (bucketLen > maxBucketLen)
					maxBucketLen = bucketLen;
			}
			averageBucketLen = (double)sum / (double)bucketSize;

			cout << "Ͱ�ĸ�����" << bucketSize << endl;
			cout << "���Ͱ�ĳ��ȣ�" << maxBucketLen << endl;
			printf("ƽ��Ͱ�ĳ��ȣ�%lf\n", averageBucketLen);
		}

		~HashTable()    // ���ڵ���vector��list��������Ҫ�����Լ��ֶ��ͷ�
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}

		}

	private:
		vector<Node*> _tables;
		int _n = 0;
		Hash hf;
	};

	void test_hash_bucket1()
	{
		HashTable<int, int> ht;
		int a[] = { 1,35,6,43,53,7,8,9,32,54};
		for (auto e : a)
		{
			ht.Insert(make_pair(e, e));
		}

		ht.Insert(make_pair(11,11));

	}

	void test_hash_bucket2()
	{
		string arr[] = { "ƻ��", "�㽶", "����", "ƻ��" };
		HashTable<string, int> countMap;
		for (auto& e : arr)
		{
			auto ret = countMap.Find(e);
			if (ret)
				ret->_kv.second++;
			else
				countMap.Insert(make_pair(e, 1));
		}
	}
	void test_hash_bucket3()                  // ����Ч�ʵĶԱ�
	{
		const size_t N = 1000000;

		set<int> s;
		unordered_set<int> u_set;
		HashTable<int, int> ht;

		vector<int> vc;
		vc.reserve(N);

		srand(time(0));
		for (int i = 0; i < N; i++)
		{
			vc.push_back(rand() + i);
		}

		// ------------------- insert��Ч�ʶԱ�  -------------------------
		size_t begin1 = clock();
		for (auto e : vc)
		{
			s.insert(e);
		}
		size_t end1 = clock();

		cout << "set Insert:" << end1 - begin1 << endl;

		size_t begin2 = clock();
		for (auto e : vc)
		{
			u_set.insert(e);
		}
		size_t end2 = clock();

		cout << "unordered_set Insert:" << end2 - begin2 << endl;

		size_t begin3 = clock();
		for (auto e : vc)
		{
			ht.Insert(make_pair(e, e));
		}
		size_t end3 = clock();

		cout << "HashTable Insert:" << end3 - begin3 << endl << endl;


		// ------------------- find��Ч�ʶԱ�  -------------------------
		size_t begin4 = clock();
		for (auto e : vc)
		{
			s.find(e);
		}
		size_t end4 = clock();

		cout << "set Find:" << end4 - begin4 << endl;

		size_t begin5 = clock();
		for (auto e : vc)
		{
			u_set.find(e);
		}
		size_t end5 = clock();

		cout << "unordered_set Find:" << end5 - begin5 << endl;

		size_t begin6 = clock();
		for (auto e : vc)
		{
			ht.Find(e);
		}
		size_t end6 = clock();

		cout << "HashTable Find:" << end6 - begin6 << endl << endl;

		cout << "�������ݵĸ���:" << u_set.size() << endl << endl;


		ht.Some();

		// ------------------- erase��Ч�ʶԱ�  -------------------------
		size_t begin7 = clock();
		for (auto e : vc)
		{
			s.find(e);
		}
		size_t end7 = clock();

		cout << "set Erase:" << end7 - begin7 << endl;

		size_t begin8 = clock();
		for (auto e : vc)
		{
			u_set.find(e);
		}
		size_t end8 = clock();

		cout << "unordered_set Erase:" << end8 - begin8 << endl;

		size_t begin9 = clock();
		for (auto e : vc)
		{
			ht.Find(e);
		}
		size_t end9 = clock();

		cout << "HashTable Erase:" << end9 - begin9 << endl;



	}

}
